package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"path/filepath"
	"shop/models"
	"strconv"
	"strings"
	"time"
)

type ShopController struct {
	beego.Controller
}

type JsonR struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *ShopController) Add() {

	img := c.GetString("img")
	name := c.GetString("name")
	price, _ := c.GetFloat("price")
	status := c.GetString("status")
	shop := models.Shop{
		Img:    img,
		Name:   name,
		Price:  price,
		Status: status,
	}
	err := models.DB.Create(&shop).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 500,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "添加成功",
			Data: shop,
		}
		c.ServeJSON()
	}
}

func (c *ShopController) Upload() {
	file, header, err := c.GetFile("img")
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 500,
			Msg:  "请选择文件并上传文件",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	defer file.Close()
	if header.Size > 2*1024*1024 {
		c.Data["json"] = JsonR{
			Code: 500,
			Msg:  "文件过大，请重新上传",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	ext := map[string]bool{
		".png": true,
		".jpg": true,
		//".jpeg": true,
	}
	suffix := strings.ToLower(filepath.Ext(header.Filename))
	if !ext[suffix] {
		c.Data["json"] = JsonR{
			Code: 500,
			Msg:  "文件类型错误",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	path := "status/img/" + header.Filename
	c.SaveToFile("img", path)
	imSrc := "http://127.0.0.1:8080/" + path
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 500,
			Msg:  "文件上传失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {

		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "文件上传成功",
			Data: imSrc,
		}
		c.ServeJSON()
	}
}

func (c *ShopController) List() {
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 5)
	pageData := make(map[string]interface{})
	key := "shop-" + strconv.Itoa(page)
	val, err := models.Rdb.Get(key).Result()
	if err != nil {
		var count int64
		models.DB.Model(&models.Shop{}).Count(&count)
		offset := (page - 1) * size
		var shop []models.Shop
		err = models.DB.Offset(offset).Limit(size).Find(&shop).Error
		if err != nil {
			c.Data["json"] = JsonR{
				Code: 500,
				Msg:  "查询失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		}
		pageData["shop"] = shop
		pageData["count"] = count
		jsonByte, _ := json.Marshal(&shop)
		value := string(jsonByte)
		models.Rdb.Set(key, value, time.Second*5)
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "查询成功—mysql",
			Data: pageData,
		}
		c.ServeJSON()
	} else {
		json.Unmarshal([]byte(val), &pageData)
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "查询成功-redis",
			Data: pageData,
		}
		c.ServeJSON()
	}
}

func (c *ShopController) Info() {
	id, _ := c.GetInt("id")
	RankKey := "Rank"
	models.Rdb.ZIncrBy(RankKey, 1, strconv.Itoa(id))
	var shop models.Shop
	key := "video" + strconv.Itoa(id)
	val, err := models.Rdb.Get(key).Result()
	if err != nil {
		err = models.DB.Where("ID=?", id).First(&shop).Error
		if err != nil {
			c.Data["json"] = JsonR{
				Code: 500,
				Msg:  "展示失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		} else {
			jsonByte, _ := json.Marshal(&shop)
			value := string(jsonByte)
			models.Rdb.Set(key, value, 0)
			c.Data["json"] = JsonR{
				Code: 200,
				Msg:  "展示成功-mysql",
				Data: shop,
			}
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &shop)
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "展示成功-redis",
			Data: shop,
		}
		c.ServeJSON()
	}
}
